package com.cyber.app_container.exception;

import com.cyber.app_container.exception.ErrorCode;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义业务异常（需要配合全局异常处理器使用）
 */
@Getter
public class BusinessException extends RuntimeException {
    private final String code;

    private final Map<String, Object> details;

    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.details = null;
    }

    public BusinessException(ErrorCode errorCode, Map<String, Object> details) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.details = details;
    }

    public static BusinessException of(ErrorCode errorCode) {
        return new BusinessException(errorCode);
    }

    public static BusinessException of(ErrorCode errorCode, String detailKey, Object detailValue) {
        Map<String, Object> details = new HashMap<>();
        details.put(detailKey, detailValue);
        return new BusinessException(errorCode, details);
    }

}
